package com.lyk.coursearrange.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyk.coursearrange.common.ServerResponse;
import com.lyk.coursearrange.common.core.base.result.R;
import com.lyk.coursearrange.common.log.annotation.Log;
import com.lyk.coursearrange.common.log.enums.OperationType;
import com.lyk.coursearrange.entity.CustomerInformation;
import com.lyk.coursearrange.entity.Job;
import com.lyk.coursearrange.entity.Teacher;
import com.lyk.coursearrange.entity.dto.CustomerInformationDto;
import com.lyk.coursearrange.entity.dto.TeacherDtoTwo;
import com.lyk.coursearrange.entity.request.PasswordVO;
import com.lyk.coursearrange.entity.request.TeacherAddRequest;
import com.lyk.coursearrange.entity.request.UserLoginRequest;
import com.lyk.coursearrange.service.TeacherService;
import com.lyk.coursearrange.service.impl.TokenService;
import com.lyk.coursearrange.util.AliyunUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lequal
 * @since 2020-03-13
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private TokenService tokenService;


    /**
     * 上传讲师证件
     * @param id
     * @param file
     * @return
     */
    @PostMapping("/upload/{id}")
    @Log(title = "上传讲师证件", operationType = OperationType.OTHER)
    public ServerResponse uploadLicense(@PathVariable("id") Integer id, MultipartFile file) {
        Map<String, Object> map = AliyunUtil.upload(file, "license");
        String license = (String) map.get("url");
        Teacher t = teacherService.getById(id);
        t.setLicense(license);
        boolean b = teacherService.updateById(t);
        if (b) {
            return ServerResponse.ofSuccess("上传证件成功");
        }
        return ServerResponse.ofError("上传证件失败");
    }



    /**
     * 讲师登录
     * @param userLoginRequest
     * @return
     */
    @PostMapping("/login")
    @Log(title = "讲师登录", operationType = OperationType.OTHER)
    public ServerResponse teacherLogin(@RequestBody UserLoginRequest userLoginRequest) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_no", userLoginRequest.getUsername());
        // 先查询是否有该账号
        Teacher teacher2 = teacherService.getOne(wrapper);
        if (teacher2 == null) {
            return ServerResponse.ofError("账号不存在");
        } else if (teacher2.getStatus() != 0) {
            return ServerResponse.ofError("账号状态异常，请联系管理员");
        }
        // 登录,使用编号登录
        Teacher teacher = teacherService.teacherLogin(userLoginRequest.getUsername(), userLoginRequest.getPassword());

        if (teacher != null) {
            // 允许登录
            String token = tokenService.getToken(teacher);
            map.put("teacher", teacher);
            map.put("token", token);
            return ServerResponse.ofSuccess(map);
        }
        // 否则一律视为密码错误
        return ServerResponse.ofError("密码错误");
    }

    /**
     * 根据id查询讲师，用于更新操作
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Log(title = "讲师登录", operationType = OperationType.SELECT)
    public ServerResponse queryTeacherById(@PathVariable("id") Integer id) {

        return ServerResponse.ofSuccess(teacherService.getById(id));
    }

    /**
     * 更新讲师
     * @param teacher
     * @return
     */
    @PostMapping("/modify")
    @Log(title = "更新讲师", operationType = OperationType.UPDATE)
    public ServerResponse modifyTeacher(@RequestBody Teacher teacher) {

        boolean b = teacherService.updateById(teacher);

        if (b) {
            return ServerResponse.ofSuccess("更新成功");
        }
        return ServerResponse.ofError("更新失败");
    }

    /**
     * 分页查询讲师

     * @return
     */
    @PostMapping ("/query")
    @Log(title = "分页查询讲师列表", operationType = OperationType.SELECT)
    public R queryTeacher(@RequestBody Map<String, String> param) {
        List<Teacher> list = teacherService.queryTeacher(param);
        Long count = teacherService.teachercount(param);
        List<TeacherDtoTwo> teacherDto = new ArrayList<>();
        list.forEach(t -> {
            teacherDto.add(TeacherDtoTwo.builder()
                            .id(t.getId())
                            .createTime(t.getCreateTime())
                            .updateTime(t.getUpdateTime())
                            .realname(t.getRealname())
                            .age(t.getAge())
                            .teach(t.getTeach())
                            .telephone(t.getTelephone())
                            .email(t.getEmail())
                            .jobtitle(t.getJobtitle())
                            .jobName(teacherService.getJobName(t.getJobtitle()))
                            .jobStatus(t.getJobStatus())
                    .build());
        });
        return R.ok()
                .data("list", teacherDto)
                .data("count", count)
                .message("查询教师列表成功");
    }






    /**
     *查询职级
     * @return
     */
    @GetMapping ("/query/job")
/*    @Log(title = "查询职级", operationType = OperationType.SELECT)*/
    public R queryTeacherJob() {
        List<Job> list = teacherService.queryTeacherJob();
        return R.ok()
                .data("list", list)
                .message("查询职级下拉框");
    }


    /**
     * 根据姓名关键字搜索讲师
     * @return
     */
    @GetMapping("/search/{page}/{keyword}")
    @Log(title = "根据姓名关键字搜索讲师", operationType = OperationType.SELECT)
    public ServerResponse searchTeacher(@PathVariable("keyword") String keyword, @PathVariable("page") Integer page,
                                        @RequestParam(defaultValue = "10") Integer limit) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("update_time");
        wrapper.like(!StringUtils.isEmpty(keyword), "realname", keyword);
        Page<Teacher> pages = new Page<>(page, limit);
        IPage<Teacher> iPage = teacherService.page(pages, wrapper);
        if (page != null) {
            return ServerResponse.ofSuccess(iPage);
        }
        return ServerResponse.ofError("查询失败!");
    }

    /**
     * 管理员根据ID删除讲师
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @Log(title = "管理员根据ID删除讲师", operationType = OperationType.DELETE)
    public ServerResponse deleteTeacher(@PathVariable Integer id) {
        boolean b = teacherService.removeById(id);
        if(b) {
            return ServerResponse.ofSuccess("删除成功！");
        }
        return ServerResponse.ofError("删除失败！");
    }

    /**
     * 用于给讲师生成讲师编号,返回一个讲师编号
     * @return
     */
    @GetMapping("/no")
    @Log(title = "用于给讲师生成讲师编号", operationType = OperationType.SELECT)
    public ServerResponse getTeacherNo() {

        List<Teacher> teacherList = teacherService.list(new QueryWrapper<Teacher>().select().orderByDesc("teacher_no"));

        // 返回最大编号的讲师编号再+1给新添加的讲师
        return ServerResponse.ofSuccess(teacherList.get(0).getTeacherNo());
    }

    /**
     * 管理员添加讲师,默认密码是123456
     * @param t
     * @return
     */
    @PostMapping("/add")
    @Log(title = "管理员添加讲师", operationType = OperationType.INSERT)
    public ServerResponse addTeacher(@RequestBody TeacherAddRequest t) {
        Teacher teacher = Teacher.builder()
                .realname(t.getRealname())
                .age(t.getAge())
                .teach(t.getTeach())
                .telephone(t.getTelephone())
                .email(t.getEmail())
                .jobtitle(t.getJobtitle())
                .jobStatus(t.getJobStatus())
                .build();
        boolean b = teacherService.addTeacher(teacher);
        if (b) {
            return ServerResponse.ofSuccess("添加讲师成功！");
        }
        return ServerResponse.ofError("添加讲师失败！");
    }

    /**
     * 管理员添加讲师,默认密码是123456
     * @param t
     * @return
     */
    @PostMapping("/update")
    @Log(title = "管理员添加讲师", operationType = OperationType.INSERT)
    public R updateTeacher(@RequestBody TeacherAddRequest t) {
        Teacher teacher = Teacher.builder()
                .id(t.getId())
                .realname(t.getRealname())
                .age(t.getAge())
                .teach(t.getTeach())
                .telephone(t.getTelephone())
                .email(t.getEmail())
                .jobtitle(t.getJobtitle())
                .jobStatus(t.getJobStatus())
                .build();
        if (teacherService.updateTeacher(teacher)) {
            return R.ok().message("修改讲师成功！");
        }
        return R.error().message("修改讲师失败！");
    }





    /**
     * 根据ID封禁、解封讲师账号，状态为0时正常，1时封禁
     * @param id
     * @return
     */
    @GetMapping("/lock/{id}")
    @Log(title = "根据ID封禁、解封讲师账号，状态为0时正常，1时封禁", operationType = OperationType.SELECT)
    public ServerResponse lockTeacher(@PathVariable("id") Integer id) {

        // 先查出来再修改，
        Teacher teacher = teacherService.getById(id);
        // 修改
        if (teacher.getStatus() == 0) {
            teacher.setStatus(1);
        } else {
            teacher.setStatus(0);
        }
        teacherService.updateById(teacher);
        return ServerResponse.ofSuccess("操作成功！");
    }


    /**
     * 修改密码
     * @param passwordVO
     * @return
     */
    @PostMapping("/password")
    @Log(title = "修改密码", operationType = OperationType.OTHER)
    public ServerResponse updatePass(@RequestBody PasswordVO passwordVO) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper();
        wrapper.eq("id", passwordVO.getId());
        wrapper.eq("password", passwordVO.getOldPass());
        Teacher teacher = teacherService.getOne(wrapper);
        if (teacher == null) {
            return ServerResponse.ofError("旧密码错误");
        }
        // 否则进入修改密码流程
        teacher.setPassword(passwordVO.getNewPass());
        boolean b = teacherService.updateById(teacher);
        if (b) {
            return ServerResponse.ofSuccess("密码修改成功");
        }
        return ServerResponse.ofError("密码更新失败");
    }

    /**
     * 查询所有讲师
     * @return
     */
    @GetMapping("/all")
    @Log(title = "查询所有讲师", operationType = OperationType.SELECT)
    public ServerResponse getAllTeacher() {

        return ServerResponse.ofSuccess(teacherService.list());
    }

}

